package string;

/**
 * 反转字符串与翻转字符串 反转字符串   i am kid  =》dik ma i 翻转字符串 i am kid -》 kid am i
 */
public class ReverseString {


  public String reverse(String str) {
    char[] charArray = str.toCharArray();
    StringBuilder reverse = new StringBuilder();
    for (int i = charArray.length - 1; i >= 0; i--) {
      reverse.append(charArray[i]);
    }
    return reverse.toString();
  }

  /**
   * 对最后的没有空格，到末尾的时候有一个判断
   */
  public String fanZhuan(String str) {
    char[] array = str.toCharArray();
    StringBuilder fanzhuan = new StringBuilder();
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < array.length; i++) {
      if (array[i] != ' ') {
        fanzhuan.append(array[i]);
        if (i == array.length - 1) {
          result.append(reverse(fanzhuan.toString()));
        }
      } else {
        //array[i]=' '
        result.append(reverse(fanzhuan.toString()));
        result.append(array[i]);
        fanzhuan = new StringBuilder();
      }

    }
    return result.toString();
  }

  public static void main(String args[]) {
    ReverseString reverseString = new ReverseString();
    String reverse = reverseString.reverse("wty is kid");
    System.out.println(reverse);
    String fanzhuan = reverseString.fanZhuan(reverse);
    System.out.println(fanzhuan);

  }


}
